package net.monare.mifss.lib.validators;

import net.monare.mifss.configurations.pojo.StorageDef;
import net.monare.mifss.exceptions.ValidatorException;
import org.apache.commons.lang.StringUtils;

import java.util.List;

/**
 * Created by kristan_uros on 9.4.2014.
 */
public class StorageDefValidator extends BaseValidator {

    public void validateStorageDefinition(StorageDef storage) throws ValidatorException {

        //
        // Check storage definition

        if (null == storage) {

            String errMessage = "StorageDef passed is null";
            logger.error(errMessage);
            throw new ValidatorException(errMessage);
        }

        if (!isAlphaNumeric(storage.getName())) {
            String errMessage = "Storage should have alphaNumeric name. Pattern: " + alphaNumericPattern;
            logger.error(errMessage);
            throw new ValidatorException(errMessage);
        }

        // check datasource name
        if (StringUtils.isEmpty(storage.getDataSourceName())) {

            String errMessage = "Storage datasource can not be empty and must be defined as Spring bean!";
            logger.error(errMessage);
            throw new ValidatorException(errMessage);
        }

        // check storage title
        if (StringUtils.isEmpty(storage.getTitle())) {

            String errMessage = "Storage title can not be empty!";
            logger.error(errMessage);
            throw new ValidatorException(errMessage);
        }

        // check for at least one pool definition
        if (storage.getContentPoolDefs().size() < 1) {

            String errMessage = "Storage should have at least one pool defined!";
            logger.error(errMessage);
            throw new ValidatorException(errMessage);
        }
        // check storage pools
        PoolValidator poolValidator = new PoolValidator();
        poolValidator.validatePools(storage);

    }

}
